package com.gopay.common.cps.dao.gsrt.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.gsrt.CustSignedConstants;
import com.gopay.common.cps.dao.gsrt.GsrtCustSignedDao;
import com.gopay.common.domain.gsrt.GsrtCustSigned;
import com.gopay.common.util.DateUtils;

/**
 * 
 * @Description: 签约
 * @author hougn
 * @date 2016年3月18日 下午7:24:52 
 *
 */
@Repository("gsrtCustSignedDao")
public class GsrtCustSignedDaoImpl extends BaseDAOImpl<GsrtCustSigned, Serializable> implements GsrtCustSignedDao {

    @Override
    public long getNextVal() {
        return Long.parseLong(this.getNextId("GSRT_CUST_SIGNED_SEQ"));
    }
    
    @Override
    public String getGopayOrderId() {
        String orderId = this.getNextId("ORDER_ID_SEQ");
        orderId = StringUtils.leftPad(orderId, 8, "0");
        orderId = DateUtils.now2string("yyyyMMdd") + orderId;
        return orderId;
    }
    
    @Override
    public GsrtCustSigned findByCustIdAndMerCustId(String custId, String merCustId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
        criteria.add(Restrictions.eq("custId", custId));
        criteria.add(Restrictions.eq("merCustId", merCustId));
        String[] signedStateArr = {"01", "03"};
        criteria.add(Restrictions.in("signedState", signedStateArr));
        List<GsrtCustSigned> orderList = this.findByCriteria(criteria);
        return orderList.size()>0 ? orderList.get(0): null;
    }
    
    @Override
    public boolean findFailByCustIdAndMerCustId(String custId, String merCustId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
        criteria.add(Restrictions.eq("custId", custId));
        criteria.add(Restrictions.eq("merCustId", merCustId));
        criteria.add(Restrictions.eq("signedState", GsrtCustSigned.SignedState.FAIL.getCode()));
        return this.findCountByCriteria(criteria) > 0;
    }
    /**
     * 只查询签约成功的数据信息
     */
    @Override
    public GsrtCustSigned findByMerCustIdAndConstracterId(String merCustId, String constracterId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
        criteria.add(Restrictions.eq("merCustId", merCustId));
        criteria.add(Restrictions.eq("constracterId", constracterId));
        criteria.add(Restrictions.eq("signedState", GsrtCustSigned.SignedState.SUCCESS.getCode()));
        List<GsrtCustSigned> orderList = this.findByCriteria(criteria);
        return orderList.size()>0 ? orderList.get(0): null;
    }
    
    @Override
    public GsrtCustSigned findByGopayOrderId(String gopayOrderId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
        criteria.add(Restrictions.eq("gopayOrderId", gopayOrderId));
        List<GsrtCustSigned> orderList = this.findByCriteria(criteria);
        return orderList.size()>0 ? orderList.get(0): null;
    }
    @Override
    public GsrtCustSigned findByMerOrderId(String merOrderId) {
    	DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
    	criteria.add(Restrictions.eq("merOrderId", merOrderId));
    	List<GsrtCustSigned> orderList = this.findByCriteria(criteria);
    	return orderList.size()>0 ? orderList.get(0): null;
    }
    
    @Override
    public GsrtCustSigned findByTransDtlSq(Long transDtlSq) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustSigned.class);
        criteria.add(Restrictions.eq("transDtlSq", transDtlSq));
        List<GsrtCustSigned> orderList = this.findByCriteria(criteria);
        return orderList.size()>0 ? orderList.get(0): null;
    }

    @Override
    public void updateGsrtCustSigned(GsrtCustSigned en){
        super.update(en);
    }

    @Override
    public List<GsrtCustSigned> findByMerAndP2PCustId(String merCustId, String p2pCustId) {
        String hql="from GsrtCustSigned where  merCustId=:merCustId and p2pCustId=:p2pCustId";
        Map<String, Object> params = new HashMap<>();
        params.put("merCustId", merCustId);
        params.put("p2pCustId", p2pCustId);
        List<GsrtCustSigned> list= this.findByHql(hql, params);
        return list;
    }
}
